package com.study.component;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author zhuzi
 * 2022/8/14 16:44
 */
@Slf4j
//@Service
public class CronThreadComponent {
    ExecutorService service = Executors.newFixedThreadPool(5);

    @Scheduled(cron = "0/1 * * * * ? ")
    public void deleteFile() {
        CompletableFuture<Void> runAsync = CompletableFuture.runAsync(() -> {
            log.info("111delete success, time{}-{}:", Thread.currentThread(), LocalDateTime.now());
            sleep();
        }, service);
        try {
            runAsync.get(11, TimeUnit.SECONDS);
        } catch (Exception e) {
            log.info("超时？？", e);
        }
    }

    private static void sleep() {
        try {
            TimeUnit.HOURS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    @Async
    @Scheduled(cron = "0/1 * * * * ? ")
    public void syncFile() {
        service.execute(() -> {

            log.info("222sync success, time{}-{}:", Thread.currentThread(), LocalDateTime.now());

        });
    }

}
